#!/bin/bash

#==============================================================================
# 脚本别名配置器 (Script Aliases Setup)
#==============================================================================
# 
# 功能描述:
#   为工作区中的脚本创建便捷的命令别名
#   支持临时和永久别名配置
#
# 作者: Pedro.Pei
# 版本: 1.0
# 更新日期: 2024
#
# 使用方法:
#   ./setup_aliases.sh
#
#==============================================================================

# 获取脚本所在目录
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
SRC_DIR="$SCRIPT_DIR/src"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

echo -e "${CYAN}🔧 脚本别名配置器${NC}"
echo ""
echo -e "${YELLOW}此工具将为您的脚本创建便捷的命令别名${NC}"
echo ""

# 定义别名
aliases=(
    "alias sm='$SCRIPT_DIR/script_manager.sh'                    # 脚本管理器"
    "alias filesort='$SRC_DIR/file_sorting_script.sh'         # 文件分类整理"
    "alias dupclean='$SRC_DIR/delete_duplicates.sh'           # 重复文件清理"
    "alias addprefix='$SRC_DIR/add_random_prefix.sh'          # 添加文件前缀"
    "alias batchrename='$SRC_DIR/batch_rename.sh'             # 批量重命名工具"
    "alias extractaudio='$SRC_DIR/extract_audio.sh'           # 音频提取"
    "alias genicons='$SRC_DIR/generate_icons.sh'              # 图标生成"
)

echo -e "${WHITE}将创建以下别名:${NC}"
echo ""
for alias_def in "${aliases[@]}"; do
    echo -e "  ${GREEN}${alias_def}${NC}"
done
echo ""

echo -e "${WHITE}选择配置方式:${NC}"
echo -e "  ${GREEN}1.${NC} 临时配置 (仅当前终端会话有效)"
echo -e "  ${GREEN}2.${NC} 永久配置 (添加到 ~/.zshrc)"
echo -e "  ${GREEN}3.${NC} 生成别名文件 (可手动source)"
echo -e "  ${GREEN}4.${NC} 取消操作"
echo ""
echo -e -n "请选择 (1-4): "
read choice

case "$choice" in
    1)
        echo ""
        echo -e "${GREEN}正在生成临时别名脚本...${NC}"
        
        # 创建临时别名文件
        temp_alias_file="/tmp/pedro_script_aliases_$$.sh"
        echo "#!/bin/bash" > "$temp_alias_file"
        echo "# Pedro Script Tools - 临时别名" >> "$temp_alias_file"
        echo "" >> "$temp_alias_file"
        
        for alias_def in "${aliases[@]}"; do
            # 提取别名定义部分（去掉注释）
            alias_cmd=$(echo "$alias_def" | cut -d'#' -f1 | xargs)
            echo "$alias_cmd" >> "$temp_alias_file"
            echo -e "  ✅ ${alias_cmd}"
        done
        
        chmod +x "$temp_alias_file"
        echo ""
        echo -e "${GREEN}✅ 临时别名脚本已生成！${NC}"
        echo -e "${YELLOW}由于shell限制，需要手动激活别名${NC}"
        echo -e "${CYAN}请复制并执行以下命令:${NC}"
        echo -e "${GREEN}source $temp_alias_file${NC}"
        echo ""
        echo -e "激活后可以使用以下命令:"
        echo -e "  ${CYAN}sm${NC}          - 启动脚本管理器"
        echo -e "  ${CYAN}filesort${NC}    - 文件分类整理"
        echo -e "  ${CYAN}dupclean${NC}    - 重复文件清理"
        echo -e "  ${CYAN}addprefix${NC}   - 添加文件前缀"
        echo -e "  ${CYAN}batchrename${NC} - 批量重命名工具"
        echo -e "  ${CYAN}extractaudio${NC} - 音频提取"
        echo -e "  ${CYAN}genicons${NC}    - 图标生成"
        ;;
    2)
        echo ""
        echo -e "${GREEN}正在添加到 ~/.zshrc...${NC}"
        echo "" >> ~/.zshrc
        echo "# Pedro's Script Tools Aliases" >> ~/.zshrc
        for alias_def in "${aliases[@]}"; do
            echo "$alias_def" >> ~/.zshrc
        done
        echo ""
        echo -e "${GREEN}✅ 永久别名设置完成！${NC}"
        echo -e "请重新启动终端或运行: ${CYAN}source ~/.zshrc${NC}"
        ;;
    3)
        alias_file="$SCRIPT_DIR/script_aliases.sh"
        echo "#!/bin/bash" > "$alias_file"
        echo "# Pedro's Script Tools Aliases" >> "$alias_file"
        echo "" >> "$alias_file"
        for alias_def in "${aliases[@]}"; do
            echo "$alias_def" >> "$alias_file"
        done
        chmod +x "$alias_file"
        echo ""
        echo -e "${GREEN}✅ 别名文件创建完成！${NC}"
        echo -e "文件位置: ${alias_file}"
        echo -e "使用方法: ${CYAN}source $alias_file${NC}"
        ;;
    4)
        echo -e "${YELLOW}操作已取消${NC}"
        ;;
    *)
        echo -e "${RED}无效选择${NC}"
        exit 1
        ;;
esac

echo ""